package servlet;

import bean.CommodityBean;
import com.alibaba.fastjson.JSON;
import dao.CommodityDao;
import entity.Commodity;
import entity.User;
import service.CommodityService;
import service.impl.CommodityServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet(name = "CommodityServlet", value = "/CommodityServlet")
public class CommodityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String opr = request.getParameter("opr");
        System.out.println(opr);
        CommodityService commodityService = new CommodityServiceImpl();
        if("selectCommodity".equals(opr)){
            String name = request.getParameter("name");
            List<Commodity> commList = commodityService.findAllByName(name);
            request.setAttribute("commList",commList);
            request.getRequestDispatcher("frontEnd/selectList.jsp").forward(request,response);
        }
        //显示所有商品信息
        if(opr.equals("msg")){
            List<CommodityBean> comBeanlist = commodityService.findAllMsg();
            response.getWriter().print(JSON.toJSONString(comBeanlist));
        }
        if ("scorePage".equals(opr)) {
            String name = request.getParameter("name");
            int curr = Integer.parseInt(request.getParameter("curr"));
            int limit = Integer.parseInt(request.getParameter("limit"));
            List<Commodity> scorePage = commodityService.findScorePage(name, ((curr - 1) * limit), limit);
            response.getWriter().print(JSON.toJSONString(scorePage));
        }
        if("commShowInfo".equals(opr)) {
            int id = Integer.parseInt(request.getParameter("id"));
            int userId = ((User) request.getSession().getAttribute("user")).getId();

            Commodity comm = commodityService.findById(id);
            request.setAttribute("comm", comm);
            request.getRequestDispatcher("CollectionServlet?opr=isExist").forward(request, response);
        }
        //按名称和类型名称查询所有商品
        if(opr.equals("NameAndClass")){
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            int curr = Integer.parseInt(request.getParameter("curr"));
            int limit = Integer.parseInt(request.getParameter("limit"));
            List<CommodityBean> ListComBean = commodityService.findAllByNameAndClass(name,type,(curr-1)*limit,limit);
            response.getWriter().print(JSON.toJSONString(ListComBean));
        }
        //按id删除商品
        if(opr.equals("delid")){
            String id = request.getParameter("id");
            if(commodityService.delId(Integer.parseInt(id))>0){
                response.getWriter().print("true");
            }else {
                response.getWriter().print("false");
            }
        }

        if ("findId".equals(opr)) {
            String id = request.getParameter("id");
            List<Commodity> idList  = commodityService.findId(Integer.parseInt(id));
            response.getWriter().print(JSON.toJSONString(idList));
        }


        if(opr.equals("countPage")){
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            int countPage = commodityService.findCountPage(name,type);
            response.getWriter().print(countPage);
        }

        //添加商品
        if(opr.equals("addAll")){
            Commodity commodity = new Commodity();
            commodity.setName(request.getParameter("name"));
            commodity.setImgUrl5(request.getParameter("image"));
            commodity.setInfo(request.getParameter("info"));
            commodity.setClassId(Integer.parseInt(request.getParameter("classId")));
            commodity.setPrice(Double.parseDouble(request.getParameter("price")));
            commodity.setNumber(Integer.parseInt(request.getParameter("diao")));
            commodity.setCount(Integer.parseInt(request.getParameter("count")));
            String date = request.getParameter("shelfTime");
            String[] dates = date.split("-");
            commodity.setShelfTime(new Date((Integer.parseInt(dates[0])-1900)
            ,(Integer.parseInt(dates[1])-1),Integer.parseInt(dates[2])));
            if(commodityService.addAll(commodity)>0){
                response.getWriter().print("<script>alert('添加成功');location.href='afterEnd/p1.jsp'</script>");
            }

        }

        if("update".equals(opr)){
            Commodity commodity = new Commodity();
            commodity.setId(Integer.parseInt(request.getParameter("id")));
            commodity.setName(request.getParameter("name"));
            commodity.setImgUrl5(request.getParameter("image"));
            commodity.setInfo(request.getParameter("info"));
            commodity.setClassId(Integer.parseInt(request.getParameter("classId")));
            commodity.setPrice(Double.parseDouble(request.getParameter("price")));
            commodity.setNumber(Integer.parseInt(request.getParameter("diao")));
            commodity.setCount(Integer.parseInt(request.getParameter("count")));
            String date = request.getParameter("shelfTime");
            String[] dates = date.split("-");
            commodity.setShelfTime(new Date((Integer.parseInt(dates[0])-1900)
                    ,(Integer.parseInt(dates[1])-1),Integer.parseInt(dates[2])));
            int result = commodityService.update(commodity);
            System.out.println(result);
            if(result>0){
                response.getWriter().print("<script>alert('修改成功');location.href='afterEnd/p1.jsp'</script>");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
